package cn.xuchunh.campusbbs.ui.fragments;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.xuchunh.campusbbs.adapter.TopicListAdapter;
import cn.xuchunh.campusbbs.base.BaseRecyclerViewAdapter;
import cn.xuchunh.campusbbs.base.BaseRefreshListFragment;
import cn.xuchunh.campusbbs.bean.Topic;
import cn.xuchunh.campusbbs.bean.User;
import cn.xuchunh.campusbbs.net.HttpRequestManager;
import cn.xuchunh.campusbbs.utils.IntentParamsKey;
import cn.xuchunh.campusbbs.utils.JsonKey;
import cn.xuchunh.campusbbs.viewholder.TopicViewHolder;

/**
 * Created by XuChunH on 2015/10/22.
 */
public class UserTopicListFragment extends BaseRefreshListFragment<TopicViewHolder, Topic> {

    private User user;

    public static UserTopicListFragment newInstance(User user) {

        Bundle args = new Bundle();
        args.putSerializable(IntentParamsKey.USER, user);
        UserTopicListFragment fragment = new UserTopicListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public UserTopicListFragment() {
    }

    @Override
    protected void afterInitView() {
        super.afterInitView();
        getRefreshLayout().setEnabled(false);
    }

    @Override
    protected RecyclerView.LayoutManager createLayoutManager() {
        return new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
    }

    @Override
    protected BaseRecyclerViewAdapter<TopicViewHolder, Topic> createAdapter() {
        return new TopicListAdapter(getActivity());
    }

    @Override
    protected List<Topic> parse(JSONObject jsonObject) throws JSONException {
        List<Topic> list = new ArrayList<>();
        JSONArray jsonArray = jsonObject.optJSONArray(JsonKey.LIST);
        int size = jsonArray.length();
        JSONObject topicJson;
        for (int i = 0; i < size; i++) {
            topicJson = jsonArray.optJSONObject(i);
            Topic topic = new Topic();
            topic.setForumId(topicJson.optInt(JsonKey.BOARD_ID));
            topic.setForumName(topicJson.optString(JsonKey.BOARD_NAME));
            topic.setId(topicJson.optInt(JsonKey.TOPIC_ID));
            topic.setTitle(topicJson.optString(JsonKey.TITLE));
            topic.setUserId(topicJson.optInt(JsonKey.USER_ID));
            topic.setUserNickName(topicJson.optString(JsonKey.USER_NICK_NAME));
            String lastReplyDate = topicJson.optString(JsonKey.LAST_REPLY_DATE);
            topic.setLastReplyDate(TextUtils.isEmpty(lastReplyDate) ? 0 : Long.valueOf(lastReplyDate));
            topic.setHot(topicJson.optInt(JsonKey.HOT) != 0);
            topic.setHits(topicJson.optInt(JsonKey.HITS));
            topic.setReplies(topicJson.optInt(JsonKey.REPLIES));
            topic.setEssence(topicJson.optInt(JsonKey.ESSENCE) != 0);
            topic.setTop(topicJson.optInt(JsonKey.TOP) != 0);
            topic.setStatus(topicJson.optInt(JsonKey.STATUS));
            topic.setSubject(topicJson.optString(JsonKey.SUBJECT));
            topic.setPicPath(topicJson.optString(JsonKey.PIC_PATH));
            list.add(topic);
        }
        int hasNext = jsonObject.optInt(JsonKey.HAS_NEXT);
        getAdapter().setHasMore(hasNext != 0);
        return list;
    }

    @Override
    protected void loadDataFromServer() {
        HttpRequestManager.getInstance(getActivity()).loadUserTopicList(
                user.getUserId(),
                getPage(),
                20,
                new ListXJsonListener(),
                new SimpleErrorListener(),
                getVolleyTag()
        );
    }

    @Override
    protected void loadDataFromCache() {

    }

    @Override
    public String getVolleyTag() {
        return UserTopicListFragment.class.getSimpleName();
    }

    @Override
    public String getCacheKey() {
        return null;
    }

    @Override
    protected void handleArgs(Bundle args) {
        if (args != null){
            user = (User) args.getSerializable(IntentParamsKey.USER);
            if(user == null){
                user = getUser();
            }
        }
    }

}
